package com.parkreport.report.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.xml.rpc.ServiceException;

import org.jfree.data.time.Day;

import com.common.jdbc.DB4ObjUtil;
import com.common.report.service.ReportService;
import com.common.report.vo.ReportCountDataVO;
import com.common.report.vo.ReportTitleVO;
import com.common.util.StringUtil;

public class LeaveCarReportService extends ReportService {

	List<ReportCountDataVO> subList = new ArrayList<ReportCountDataVO>();
	
	@Override
	public List findDatas(HttpServletRequest req) throws ServiceException {
		startDate = req.getParameter("startDate");
		endDate = req.getParameter("endDate");
		if(StringUtil.isBlank(startDate)&&StringUtil.isBlank(endDate)){
			Day d = new Day();
			String date = d.getYear()+"-"+StringUtil.keeperStr(String.valueOf(d.getMonth()), 2, "0")+"-"+d.getDayOfMonth();
			startDate =date;
			endDate = date;
			req.setAttribute("startDate", startDate);
			req.setAttribute("endDate", endDate);
		}
		List<ReportCountDataVO> list = new ArrayList<ReportCountDataVO>();
		
		List<ReportTitleVO> mentList =  findMents(req);
		for (int i = 0; i < mentList.size(); i++) {
			String areaName = mentList.get(i).getCode();
			BigDecimal c1 = new BigDecimal(0);
			BigDecimal c2 = new BigDecimal(0);
			List<ReportCountDataVO> subList = new ArrayList<ReportCountDataVO>();
			if("合计".equals(areaName)){
				subList = findalltotal(startDate,endDate);
			}else{
				subList = findsub(areaName,startDate,endDate);
			}
			for (int j = 0; j < subList.size(); j++) {
				ReportCountDataVO v = subList.get(j);
				if("1".equals(v.getTitles())){
					c1=v.getCounts();
				}else{
					c2=v.getCounts();
				}
			}
			ReportCountDataVO vo = new ReportCountDataVO();
			vo.setMents(areaName);
			vo.setCounts(c1.subtract(c2));
			list.add(vo);
		}
		
		return list;
	}
	
	public List<ReportCountDataVO> findsub(String areaName,String startDate,String endDate){
		StringBuilder sql = new StringBuilder("");
		sql.append("select count(*) as counts, pd.type as titles from LPR_RECORD lpr,QUERY_DMB qd,PARK_DMB pd,PARK_AREA pa");
		sql.append(" where 1=1");
		sql.append(" and lpr.RQSJ>='"+startDate+" 00:00:00' and lpr.RQSJ<='"+endDate+" 23:59:59'");
		sql.append(" and lpr.NID = qd.NID  and qd.NID = pd.NID");
		sql.append(" and pd.PARK_AREA_ID=pa.ID");
		sql.append(" and pa.area_name='"+areaName+"'");
		sql.append(" group by pd.type");
		List<ReportCountDataVO> list = DB4ObjUtil.execute4Obj(new ReportCountDataVO(), sql.toString());
		//list.addAll(findalltotal(startDate,endDate));
		return list;
	}
	
	
	public List<ReportCountDataVO> findalltotal(String startDate,String endDate) {
		StringBuilder sql = new StringBuilder("");
		sql.append("select count(*) as counts, pd.type as titles from LPR_RECORD lpr,QUERY_DMB qd,PARK_DMB pd,PARK_AREA pa");
		sql.append(" where 1=1");
		sql.append(" and lpr.RQSJ>='"+startDate+" 00:00:00' and lpr.RQSJ<='"+endDate+" 23:59:59'");
		sql.append(" and lpr.NID = qd.NID  and qd.NID = pd.NID");
		sql.append(" and pd.PARK_AREA_ID=pa.ID");
		sql.append(" group by pd.type");
		List<ReportCountDataVO> list = DB4ObjUtil.execute4Obj(new ReportCountDataVO(), sql.toString());
		return list;
	}
	
	@Override
	public List<ReportTitleVO> findTitles(HttpServletRequest req) throws ServiceException {
		List<ReportTitleVO> list = new ArrayList<ReportTitleVO>();
		list.add(new ReportTitleVO("总数","总数"));
		return list;
	}
	
	@Override
	public List<ReportTitleVO> findMents(HttpServletRequest req) throws ServiceException {
		StringBuilder sql = new StringBuilder("");
		sql.append("select AREA_NAME as code,AREA_NAME as name from park_area where PID<>'0'");
		List<ReportTitleVO> list = DB4ObjUtil.execute4Obj(new ReportTitleVO(), sql.toString());
		
		list.add(new ReportTitleVO("合   计","合计"));
		return list;
	}
}
